﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_Information")]
    public class P_Information : IEntity<P_Information>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Title { get; set; }
        [Column(TypeName = "nvarchar")]
        public string Content { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(50)]
        public string InfoFrom { get; set; }
        /// <summary>
        /// 资迅类型
        /// </summary>
        public int InfoType { get; set; }
        public int Status { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string CreateUser { get; set; }
        public DateTime CreateTime { get; set; }

        public override P_Information Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_Information Find(System.Linq.Expressions.Expression<Func<P_Information, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Information.FirstOrDefault(where);
                if (item != null)
                {
                    this.ID = item.ID;
                    this.Content = item.Content;
                    this.Title = item.Title;
                    this.InfoFrom = item.InfoFrom;
                    this.InfoType = item.InfoType;
                    this.Status = item.Status;
                    this.CreateTime = item.CreateTime;
                    this.CreateUser = item.CreateUser;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_Information, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Information.Any(where);
            }
        }
    }
}